<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------

namespace app\user\controller;

use cmf\controller\AdminBaseController;
use app\wechat\controller\User;
use think\Db;


class AdminSendController extends AdminBaseController
{


    public function index()
    {
        ini_set('max_execution_time', 0);
        

        $part = '区纪委监委、区委办公室、区委组织部、区委宣传部、区委统战部、区委政法委、区编委办公室、区直属机关党委、区信访局、区府办公室、区财政局、区招商办、琶洲管委会、区机关事务局、区人大、区政协、区总工会、团区委、区妇联、区侨联';
        $part = explode('、',$part);
        $ids = Db::name('wechat_department')->where('name','in',$part)->order('id')->column('id,name');
        $this->assign('part',$ids);

        return $this->fetch();
    }
    public function indexcheck()
    {
        $part_id = $this->request->param('part_id',0,'intval');
        $info = Db::name('member')
        ->alias('a')
        ->join('wechat_department_user b','a.openid=b.user_id','left')
        ->where(['b.department_id'=>$part_id])
        ->column('a.openid,a.user_login');
        echo json_encode($info);
    }
    public function addPost()
    {
        $content = $this->request->param('content','','trim');
        if (empty($content)) {
            $this->error('信息内容不能为空');
        }
        $part_id = $this->request->param('part_id',0,'intval');
        $user_id = $this->request->param('user',0,'intval');
        $user = new User();
        if ($part_id == 0) {
            $part = '区纪委监委、区委办公室、区委组织部、区委宣传部、区委统战部、区委政法委、区编委办公室、区直属机关党委、区信访局、区府办公室、区财政局、区招商办、琶洲管委会、区机关事务局、区人大、区政协、区总工会、团区委、区妇联、区侨联';
            $part = explode('、',$part);
            $ids = Db::name('wechat_department')->where('name','in',$part)->order('id')->column('id');
            $part_id = implode("|", $ids);
            $result = $user->send_text($part_id,'',$content);
        }else{
            if ($user_id == 0) {
                $result = $user->send_text($part_id,'',$content);
            }else{
                $result = $user->send_text('',$user_id,$content);
            }
        }

        if ($result) {
            Db::name('send_log')->insert(
                [
                    'part_id'=>$part_id,
                    'user_id'=>$user_id,
                    'time'=>time(),
                    'content'=>$content,
                ]
            );
            $this->success('发送成功');
        }else{
            $this->error('发送失败,服务器异常');
        }
        
    }

    //私信日志
    public function send_log()
    {
        $part = '区纪委监委、区委办公室、区委组织部、区委宣传部、区委统战部、区委政法委、区编委办公室、区直属机关党委、区信访局、区府办公室、区财政局、区招商办、琶洲管委会、区机关事务局、区人大、区政协、区总工会、团区委、区妇联、区侨联';
        $part = explode('、',$part);
        $ids = Db::name('wechat_department')->where('name','in',$part)->order('id')->column('id,name'); 
        $this->assign('parts',$ids);

        $user = Db::name('member')->column('id,user_login'); 
        $this->assign('user',$user);
        $post = $this->request->param();
        $between_start = strtotime(date('Y-m'));

        $between_end = strtotime('+1 month',strtotime(date('Y-m')))-(60*60*24);
        $where['time']  = ['between',[$between_start,$between_end]];
        $this->assign('start_time',date("Y-m-d",$between_start));
        $this->assign('end_time',date("Y-m-d",$between_end));
        $this->assign('between',['start'=>$between_start,'end'=>$between_end]);
        if (!empty($post['part_id'])) {
            $where['part_id'] = $post['part_id'];
        }

        if ( !empty($post['start_time']) && !empty($post['end_time'])) {
            $end = strtotime($post['end_time']);
            $start =  strtotime($post['start_time']);
            $where['time']  = ['between',[$start,$end]];
            $this->assign('start_time',$post['start_time']);
            $this->assign('end_time',$post['end_time']);
        }

        $data = Db::name('send_log')->where($where)->select();
        $this->assign('data',$data);
        return $this->fetch();
    }
}
